<template>
  <div class="container">
    <el-dialog
      :title="text === 'add' ? '新增目录' : '修改目录'"
      :visible.sync="dialogVisible"
      width="25%"
      :before-close="handleClose"
    >
      <el-form
        :label-position="labelPosition"
        label-width="80px"
        :model="form"
        ref="form"
        :rules="rules"
      >
        <el-form-item label="所属学科" prop="subjectID">
          <el-select
            v-model.number="form.subjectID"
            placeholder="请选择"
            style="width: 262px"
          >
            <el-option
              :label="item.subjectName"
              :value="item.id"
              v-for="(item, index) in discipline"
              :key="index"
            ></el-option>
            <!-- <el-option label="区域二" value="beijing"></el-option> -->
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="所属学科" prop="title">
          <el-input v-model="form.title" placeholder="请选择"></el-input>
        </el-form-item> -->
        <el-form-item label="目录名称" prop="tagName">
          <el-input
            v-model="form.tagName"
            placeholder="请输入目录名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-row>
        <el-col :span="24" :push="14">
          <el-button size="medium" @click="handleClose">取消</el-button>
          <el-button type="primary" size="medium" @click="submit"
            >确认</el-button
          >
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/tags.js'
import { list } from '@/api/hmmm/subjects.js'
export default {
  props: {
    text: {
      type: String
    }
  },
  data () {
    return {
      discipline: [],
      labelPosition: 'right',
      dialogVisible: false,
      form: {
        subjectID: '',
        tagName: ''
      },
      rules: {
        subjectID: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }
        ],
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 关闭弹窗
    handleClose () {
      this.$refs.form.resetFields()
      this.form = {
        subjectID: '',
        tagName: ''
      }
      this.dialogVisible = false
    },
    // 提交
    async submit () {
      try {
        await this.$refs.form.validate()
        if (this.text === 'add') {
          console.log(this.form)
          const data = await add(this.form)
          console.log(data)
          this.$parent.getList()
          this.handleClose()
        } else {
          await update(this.form)
          // console.log(data)
          this.$parent.getList()
          this.handleClose()
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 获取所属科目列表
    async subject () {
      const { data } = await list()
      console.log(data.items)
      this.discipline = data.items
      // console.log(res)
      // console.log(this.discipline)
    }
  }
}
</script>

<style scoped lang="less"></style>
